package cloud.pangui.core.mongo.operate;

import cloud.pangui.core.mongo.element.Cond;
import org.bson.Document;

/**
 * @Auther hepangui
 * @Date 2020/10/06 0001
*/
public class Delete {


	/**
	 * 条件
	 */
	protected Where where;

	/**
	 * 查询的是哪个表
	 */
	protected String collectionName;

	/**
	 * 不允许直接new，请使用 from方法
	 */
	protected Delete() {

	}

	/**
	 * 设置表名
	 *
	 * @param fromName
	 * @return
	 */
	public static Delete from(String fromName) {
		Delete delete = new Delete();
		delete.collectionName = fromName;
		return delete;
	}

	/**
	 * 根据sql获取一个delete
	 * @param sql
	 * @return
	 */
	public static SqlDelete fromSql(String sql){
		SqlDelete sqlDelete = new SqlDelete(sql);
		return sqlDelete;
	}

	/**
	 * 简单的where条件，直接传cond数组即可，使用and连接
	 * @param conds
	 * @return
	 */
	public Delete where(Cond... conds) {
		this.where = new Where(Where.SIMPLE_MODE);
		if (conds != null) {
			this.where.and(conds);
		}
		return this;
	}

	/**
	 * 自定义查询条件
	 *
	 * @param where
	 * @return
	 */
	public Delete where(Where where) {
		this.where = where;
		if (where.getMode() != Where.SIMPLE_MODE) {
			throw new Where.CondNotSupportInSimpleModeException("delete时只允许使用简单查询条件");
		}
		return this;
	}

	public String getCollectionName(){
		return collectionName;
	}

	public Document getWhereDoc(){
		return where.build();
	}
}
